package com.ruoyi.business.sundry.domain.vo;

import cn.hutool.core.util.NumberUtil;
import com.ruoyi.common.annotation.Excel;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

@Data
public class JbUserCommissionVo
{
    /**
     * 用户id
     */
    private Long userId;
    /**
     * 部门id
     */
    private Long deptId;
    /**
     * 用户名称
     */
    @Excel(name = "姓名")
    private String userName;
    /**
     * 部门名称
     */
    @Excel(name = "所属部门")
    private String deptName;
    /**
     * 时间，到月份
     */
    @Excel(name = "时间")
    private String yearMonth;
    /**
     * 新增订单
     */
    @Excel(name = "成交订单")
    private Integer orderAdd = 0;
    /**
     * 成交金额
     */
    @Excel(name = "成交金额")
    private BigDecimal orderTotalAmount = BigDecimal.ZERO;
    /**
     * 回款金额
     */
    @Excel(name = "回款金额")
    private BigDecimal orderReceivedAmount = BigDecimal.ZERO;
    /**
     * 订单提成
     */
    private BigDecimal commissionAmount = BigDecimal.ZERO;

    /**
     * 订单预提成合计
     */
    @Excel(name = "订单预提成")
    private BigDecimal commissionLastAmount = BigDecimal.ZERO;

    /**
     * 奖金提成
     */
    @Excel(name = "奖金提成")
    private BigDecimal bonusCommissionLastAmount = BigDecimal.ZERO;

    public void getCommissionCalculation(List<Map> ruleList)
    {
        this.commissionLastAmount=commissionAmount;
        if (ObjectUtils.isEmpty(ruleList))
        {
            return;
        }
        BigDecimal ratioValue=null;
        for (Map map : ruleList)
        {
            // 开始值
            BigDecimal startValue = NumberUtil.toBigDecimal(map.get("startValue").toString());
            // 结束值
            BigDecimal endValue = NumberUtil.toBigDecimal(map.get("endValue").toString());
            //startValue 的值转成万
            startValue = startValue.multiply(BigDecimal.valueOf(10000.00));
            endValue = endValue.multiply(BigDecimal.valueOf(10000.00));
            if (this.getOrderTotalAmount().compareTo(startValue) >= 0 && this.getOrderTotalAmount().compareTo(endValue) < 0)
            {
                // 提成比率
                ratioValue = NumberUtil.toBigDecimal(map.get("ratioValue").toString());
                break;
            }
        }
        if(ObjectUtils.isNotEmpty(ratioValue))
        {
            //ratioValue 换成百分比
            ratioValue = ratioValue.divide(BigDecimal.valueOf(100.00));
            this.bonusCommissionLastAmount = this.getOrderTotalAmount().multiply(ratioValue)
                    .setScale(2, RoundingMode.UP);
        }
    }
}
